//
//  PostImgTableViewCell.m
//  BusineseStore
//
//  Created by 闪风猫 on 2017/11/25.
//  Copyright © 2017年 TNG. All rights reserved.
//

#import "PostImgTableViewCell.h"

@implementation PostImgTableViewCell

static NSString * const ChoosePhotoCollectionViewCellID = @"ChoosePhotoCollectionViewCell";

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.collectionView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsMake(0, 0, 0, MAXFLOAT);
    }
    return self;
}
-(UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout * layout= [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize=CGSizeMake((WIDTH - 75)/4, (WIDTH - 75)/4);
        layout.minimumLineSpacing = 15;
        layout.minimumInteritemSpacing = 15;
        layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH ,((WIDTH - 75)/4 + 25) * (self.imgArr.count / 4 + 1)) collectionViewLayout:layout];
        _collectionView.delegate=self;
        _collectionView.dataSource=self;
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = SFMWhiteColor;
        _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        [_collectionView registerClass:[ChoosePhotoCollectionViewCell class] forCellWithReuseIdentifier:ChoosePhotoCollectionViewCellID];
    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgArr.count+1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChoosePhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ChoosePhotoCollectionViewCellID forIndexPath:indexPath];
    if (indexPath.row == self.imgArr.count)
    {
        [cell.img setImage:[UIImage imageNamed:@"plus.png"]];
        cell.closeBtn.hidden = YES;
    }
    else{
        [cell.img setImage:self.imgArr[indexPath.row]];
        cell.closeBtn.hidden = NO;
    }
    cell.closeBtn.tag = indexPath.row;
    [cell.closeBtn addTarget:self action:@selector(closeBtnClik:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (indexPath.row == self.imgArr.count) {
        if ([self.delegate respondsToSelector:@selector(sendPostImgTableViewCellAddImgEvent:)]) {
            [self.delegate sendPostImgTableViewCellAddImgEvent:indexPath.row];
        }
    }else
    {
        BOOL isVideo = NO;
        id asset = _selectedAssets[indexPath.item];
        if ([asset isKindOfClass:[PHAsset class]]) {
            PHAsset *phAsset = asset;
            isVideo = phAsset.mediaType == PHAssetMediaTypeVideo;
        } else if ([asset isKindOfClass:[ALAsset class]]) {
            ALAsset * alAsset = asset;
            isVideo = [[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo];
        }
        if(isVideo)
        {
            TZVideoPlayerController *vc = [[TZVideoPlayerController alloc] init];
            TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypeVideo timeLength:@""];
            vc.model = model;
            self.watchVidowBlock(vc);
        }else
        {
            self.seeImgBlock(indexPath.item);
        }
    }
}

-(void)sendImgArr:(NSMutableArray *)imgArr
{
    self.imgArr = imgArr;
    WeakSelf;
    self.collectionView.frame = CGRectMake(0, 0, WIDTH ,((WIDTH - 75)/4 + 10)* (self.imgArr.count / 4 + 1));
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.collectionView reloadData];
    });
}

-(void)closeBtnClik:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(sendPostImgTableViewCellDeleteEvent:)]) {
        [self.delegate sendPostImgTableViewCellDeleteEvent:btn.tag];
    }
}
@end
